var usernameCookie = "igoal_un";
var passwordCookie = "igoal_pw";

function login(username, password, remember) {
	if (username == "tài khoản...") {
		username = "";
	}
	if (password == "mật khẩu...") {
		password = "";
	}

	// store user's login to cookie
	if (remember) {
		if (username != "" && password != "") {
			setCookie(usernameCookie, encryptData(username), 30);
			setCookie(passwordCookie, encryptData(password), 30);
		}
	}

	// login
	var obj = {
		username : username,
		password : password
	};
	postData("login.html", obj);
}

function getUsernameFromCookie() {
	getCookie(usernameCookie);
}

function getPasswordFromCookie() {
	
}

function postData(url, obj) {
	var frm = document.createElement("form");
	frm.method = "post";
	frm.action = url;
	for (var attribute in obj) {
		var inp = document.createElement("input");
		inp.setAttribute("name", attribute);
		inp.setAttribute("value", obj[attribute]);
		frm.appendChild(inp);
	}

	document.body.appendChild(frm);
	frm.submit();
	document.body.removeChild(frm);
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value)
			+ ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1)
				c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}
